import "./theme/ssr-safe.ts"; // 解决watch(immediate) 报错
import { defineConfig } from "vitepress";
import path from "path"; // 引入 path工具，用于配置路径别名
import llmstxt from "vitepress-plugin-llms";
import { teekConfig } from "./teekConfig";
// 本地 Teek 主题包引用（与 Teek 在线主题包引用 二选一）
import { version } from "../../packages/teek/version";

// Teek 在线主题包引用（需安装 Teek 在线版本）
// import { version } from "vitepress-theme-teek/es/version";

const description = [
  // 自定义站点描述~  需要自定义修改内容
  "🗼共灵知塔",
  "白木的知识塔~🎉",
  "灵阈研都是一座以「灵异」和「阈限」为核心概念的研究都市。在这里，我们把「灵异」拆解为可验证的变量，把「阈限」转化为可观测的维度，致力于探索未知、验证真相、突破极限",
].toString();

// 使用 llmstxt 插件，确保 TS 认为它被使用
const llmPlugin = llmstxt({
  /* 你的插件配置 */
});

// 使用 version 注入到全局，或打印，让 TS 不报未使用
console.log("[Teek Version]:", version);

// https://vitepress.dev/reference/site-config
export default defineConfig({
  extends: teekConfig,
  // 修改网站导航栏上的站点标题
  title: "🗼共灵知塔💕",
  description: description,
  cleanUrls: false,
  lastUpdated: true,
  lang: "zh-CN",
  head: [
    // 站点信息配置，会出现在搜索引擎中，需要自定义修改内容
    // ["link", { rel: "icon", type: "image/svg+xml", href: "https://mu.baimu.live/a/img/shiroki-logo-mini.svg" }],
    ["link", { rel: "icon", type: "image/png", href: "/ziyuan/img/ico-baimu.png" }], //更换浏览器标签的图标1️⃣，尽量用本地图片，否则会不显示
    ["meta", { property: "og:type", content: "website" }],
    ["meta", { property: "og:locale", content: "zh-CN" }],
    ["meta", { property: "og:title", content: "🗼共灵知塔 | 灵阈研都" }],
    ["meta", { property: "og:site_name", content: "一座不设围墙的研究都市" }],
    ["meta", { property: "og:image", content: "/ziyuan/img/ico-baimu.png" }], //更换浏览器标签的图标，需要与1️⃣配置项同步，尽量用本地图片，否则会不显示
    ["meta", { property: "og:url", content: "https://baimu.live" }],
    ["meta", { property: "og:description", description }],
    ["meta", { name: "description", description }],
    ["meta", { name: "author", content: "白木" }],
    // 禁止浏览器缩放
    // [
    //   "meta",
    //   {
    //     name: "viewport",
    //     content: "width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no",
    //   },
    // ],

    // 需要自定义修改内容
    ["meta", { name: "keywords", description }],

    ["meta", { name: "baidu-site-verification", content: "codeva-GdK2q9MO1i" }], // 百度收录

    ["meta", { name: "msvalidate.01", content: "48CABE70F538B8D117567176ABF325AF" }], // Bing 收录验证

    ["script", { charset: "UTF-8", id: "LA_COLLECT", src: "//sdk.51.la/js-sdk-pro.min.js" }], // 51LA统计 - api

    [
      "script",
      {},
      // 需要自定义51.LA的统计代码🛰️🫸🏻平台内，选择同步安装（推荐）
      `typeof LA !== 'undefined' && LA.init({ id: "3LQTKPXwllrMFIlo", ck: "3LQTKPXwllrMFIlo", hashMode: true })`,
    ], // 51.la
  ],
  markdown: {
    // 开启行号
    lineNumbers: true,
    image: {
      // 默认禁用；设置为 true 可为所有图片启用懒加载。
      lazyLoading: true,
    },
    // 更改容器默认值标题
    container: {
      tipLabel: "提示",
      warningLabel: "警告",
      dangerLabel: "危险",
      infoLabel: "信息",
      detailsLabel: "详细信息",
    },
  },
  sitemap: {
    // 需要自定义修改内容
    hostname: "https://baimu.live",
    transformItems: items => {
      const permalinkItemBak: typeof items = [];
      // 使用永久链接生成 sitemap
      const permalinks = (globalThis as any).VITEPRESS_CONFIG.site.themeConfig.permalinks;
      items.forEach(item => {
        const permalink = permalinks?.map[item.url];
        if (permalink) permalinkItemBak.push({ url: permalink, lastmod: item.lastmod });
      });
      return [...items, ...permalinkItemBak];
    },
  },
  themeConfig: {
    // https://vitepress.dev/reference/default-theme-config
    // 修改站点导航栏上的LOGO ， 导航栏LOGO ，  需要自定义修改内容
    logo: "https://mu.baimu.live/a/img/ico-baimu.png",
    darkModeSwitchLabel: "主题",
    sidebarMenuLabel: "菜单",
    returnToTopLabel: "返回顶部",
    lastUpdatedText: "上次更新时间",
    outline: {
      level: [2, 4],
      label: "本页导航",
    },
    docFooter: {
      prev: "上一页",
      next: "下一页",
    },
    nav: [
      // 导航栏配置表  ，  需要自定义修改内容
      // img  是图标配置，可以注释取消
      // text 是标题
      // <span> 是可以点击跳转的文本
      // link 是链接
      { text: "灵阈研都", link: "https://baimu.live/" },

      // 分组的导航栏1 - 起始点
      {
        text: "😒不许点",
        items: [
          {
            // 分组标题1
            text: "笨蛋",
            items: [
              {
                text: `
                  <div style="display: flex; align-items: center; gap: 4px;">
                  <!-- img是图标配置，可以注释取消 -->
                    <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                    <span>🪄</span>
                  </div>
                  `,
                link: "/shiroki/d0cny",
              },
            ],
          },

          {
            // 分组标题2
            text: "为什么点开？",
            items: [
              {
                text: `
                  <div style="display: flex; align-items: center; gap: 4px;">
                    <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                    <span>✨</span>
                  </div>
                  `,
                link: "/shiroki/d0cny",
              },
            ],
          },

          {
            // 分组标题3
            text: "吖豁",
            items: [
              {
                text: `
                  <div style="display: flex; align-items: center; gap: 4px;">
                    <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                    <span>👋</span>
                  </div>
                  `,
                link: "/shiroki/d0cny",
              },
            ],
          },
        ],
      },
      // 分组的导航栏1 - 终止点

      // 分组的导航栏2 - 起始点
      {
        text: "🕵🏻知识大脑",
        items: [
          {
            // 分组标题1
            text: "知识库",
            items: [
              {
                text: `
                  <div style="display: flex; align-items: center; gap: 4px;">
                    <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                    <span>📑一些个人知识</span>
                  </div>
                  `,
                link: "/glzt/tbwv6",
              },
            ],
          },

          {
            // 分组标题2
            text: "游戏领域",
            items: [
              {
                text: `
                  <div style="display: flex; align-items: center; gap: 4px;">
                    <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                    <span>✨一些游戏行业相关</span>
                  </div>
                  `,
                link: "/games/zd4ez",
              },
            ],
          },

          {
            // 分组标题3
            text: "web领域",
            items: [
              {
                text: `
                  <div style="display: flex; align-items: center; gap: 4px;">
                    <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                    <span>⌨️代码与建站知识</span>
                  </div>
                  `,
                link: "/",
              },
            ],
          },

          {
            // 分组标题4
            text: "Vitepressteek",
            items: [
              {
                text: `
                  <div style="display: flex; align-items: center; gap: 4px;">
                    <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                    <span>🎨与Vitepress-teek相关</span>
                  </div>
                  `,
                link: "/vp-teek/8wdsr",
              },
            ],
          },
        ],
      },
      // 分组的导航栏2 - 终止点

      // 分组的导航栏3 - 起始点
      {
        text: "🙇🏻参见Teek",
        items: [
          {
            // 分组标题1
            text: "归档页🗃️",
            items: [
              {
                text: `
                <div style="display: flex; align-items: center; gap: 4px;">
                  <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                  <span>点击预览</span>
                </div>
                `,
                link: "/archives",
              },
            ],
          },

          {
            // 分组标题2
            text: "清单页📰",
            items: [
              {
                text: `
                <div style="display: flex; align-items: center; gap: 4px;">
                  <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                  <span>点击预览</span>
                </div>
                `,
                link: "/articleOverview",
              },
            ],
          },

          {
            // 分组标题3
            text: "登录页🔱",
            items: [
              {
                text: `
                <div style="display: flex; align-items: center; gap: 4px;">
                  <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                  <span>点击预览</span>
                </div>
                `,
                link: "/login",
              },
            ],
          },

          {
            // 分组标题4
            text: "风险链接提示页⚠️",
            items: [
              {
                text: `
                <div style="display: flex; align-items: center; gap: 4px;">
                  <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                  <span>点击预览</span>
                </div>
                `,
                link: "/risk-link?target=http://baimu/live",
              },
            ],
          },

          {
            // 分组标题5
            text: "分类页📃",
            items: [
              {
                text: `
                <div style="display: flex; align-items: center; gap: 4px;">
                  <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                  <span>点击预览</span>
                </div>
                `,
                link: "/categories",
              },
            ],
          },

          {
            // 分组标题6
            text: "标签页🔖",
            items: [
              {
                text: `
                <div style="display: flex; align-items: center; gap: 4px;">
                  <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                  <span>点击预览</span>
                </div>
                `,
                link: "/tags",
              },
            ],
          },
        ],
      },
      // 分组的导航栏3 - 终止点

      // 分组的导航栏4 - 起始点
      {
        text: "🥂干杯",
        items: [
          {
            // 分组标题1
            text: "🥰打赏",
            items: [
              {
                text: `
                <div style="display: flex; align-items: center; gap: 4px;">
                  <img src="https://mu.baimu.live/a/img/qrcode/alipay.jpg" alt="" style="width: 16px; height: 16px;">
                  <span>点击进入打赏</span>
                </div>
                `,
                link: "/personal/",
              },
            ],
          },

          {
            // 分组标题1
            text: "🎈Teek的伙伴们",
            items: [
              {
                text: `
                <div style="display: flex; align-items: center; gap: 4px;">
                  <img src="https://mu.baimu.live/a/img/ico-baimu.png" alt="" style="width: 16px; height: 16px;">
                  <span>全球Teek交流峰会</span>
                </div>
                `,
                link: "/bm/f9z98.html",
              },
            ],
          },

          // 新增分组标题...
        ],
      },
      // 分组的导航栏4 - 终止点

      { text: "", link: "" },
    ],
    // 导航栏上的 Gitee 图标配置，需要自定义修改内容
    socialLinks: [{ icon: "gitee", link: "https://gitee.com/Misaka273/vitepress-teek-shiroki" }],

    // 本地搜索配置 - 不能搜索全局文本内容，只能搜索标题一类的
    search: {
      provider: "local",
      options: {
        translations: {
          button: {
            buttonText: "搜索相关设置",
            buttonAriaLabel: "输入您想搜索相关设置",
          },
          modal: {
            noResultsText: "无法找到相关结果",
            resetButtonTitle: "清除查询条件",
            footer: {
              navigateText: "选择",
              selectText: "跳转",
              closeText: "关闭弹窗",
            },
          },
        },
      },
    }, // 开启下方的 algolia 搜索，请注释/删除本地搜索配置

    // 全局文本的搜索栏配置项，能搜索标题、文本，需要搭配algolia
    // search: {
    //   provider: "algolia",
    //   options: {
    //     appId: "2LFTZ9LKO9",
    //     apiKey: "017332fa7dc0bbe5e1637b215f92a5d1",
    //     indexName: "vitepress_theme_teek",
    //   },
    // },

    editLink: {
      // 文章页面评论下的配置，需要自定义修改内容
      text: "在 GitHub 上编辑此页",
      pattern: "https://github.com/Misaka273",
    },
  },
  vite: {
    resolve: {
      alias: {
        "@shiroki": path.resolve(__dirname, "../../packages/shiroki"),
      },
    },
    plugins: [
      llmPlugin, // 以确保llmstxt 插件生效
      llmstxt() as any,
    ],
    // 扩展 plugins 时，预留llmPlugin，避免失效
    // plugins: [
    //   llmPlugin, // llmstxt 插件生效
    // ],
  },
  // transformHtml: (code, id, context) => {
  //   if (context.page !== "404.md") return code;
  //   return code.replace("404 | ", "");
  // },
});
